// pages/homePage/homePage.js
var app = getApp();
var provinces = []; //省s
var provinceNames = []; //省名称
var citys = []; //城市s
var cityNames = []; //城市名称
var countys = []; //区县s
var countyNames = []; //区县名称
var province = "" + 101101; //省
var city = "" + 101101101; //城市
var county = "" + 101101101101; //区县
Page({
  /**
   * 页面的初始数据
   */
  data: {
    multiArray: [],
    multiIndex: [0, 0, 0],
    addresss: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getUserLocation();
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight,
      price:options.price,
      orderList:options.orderList,
      partnerUserId: options.partnerUserId
    })
    this.getProvinceData();
  },
  // /**
  //  * 页面分享
  //  */
  // onShareAppMessage: function () {
  //   var that = this;
  //   return {
  //     title: that.data.title,
  //     desc: '分享页面的内容',
  //     imageUrl: "http://file.jwsxmall.com/shareImg.jpg",
  //     path: '/pages/communityGroupApplyage/communityGroupApply?partnerUserId=' + wx.getStorageSync("userId") + '&type=2', // 路径，传递参数到指定页面
  //   }
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
        } else {
          wx.navigateTo({
            url: '/pages/zcuslist/index?partnerUserId=' + that.data.partnerUserId + '&fan=1'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  navBack: function () {
    wx.navigateBack({

    })
  },
  
  // 获取省份数据
  getProvinceData: function () {
    var that = this;
    var s;
    wx.request({
      url: app.globalData.url + 'getAddressLib', //
      data: {
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var arr = res.data.data;
        // 更新数据  
        for (var i = 0; i < arr.length; i++) {
          provinceNames[i] = arr[i].province;
          provinces[i] = arr[i].code;
        }

        that.setData({
          provinces: provinces,
          provinceNames: provinceNames

        })
        that.getCityArr(provinces[0]);
      },
      fail: function (err) {

      },
    })
  },

  // 获取城市数据
  getCityArr: function (arr, count = 0) {
    var that = this;
    var c;
    wx.request({
      url: app.globalData.url + 'getAddressCityLib', //
      data: {
        parent: arr
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var arr = res.data.data;
        for (var i = 0; i < arr.length; i++) {
          cityNames[i] = arr[i].city;
          citys[i] = arr[i].code;
        }
        // 更新数据  
        that.setData({
          citys: citys,
          cityNames: cityNames
        })
        city = citys[0];
        that.getCountyInfo(city, 0);
      },
      fail: function (err) {

      },
    })
    if (citys.length == 0) {
      citys[0] = {
        name: ''
      };
      cityNames[0] = {
        name: ''
      };
    }

  },

  // 获取区县数据
  getCountyInfo: function (arr, column = 0) {
    var that = this;
    var c;
    countys = [];
    countyNames = [];
    wx.request({
      url: app.globalData.url + 'getAddressCountyLib', //
      data: {
        parent: arr
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var arr = res.data.data;
        for (var i = 0; i < arr.length; i++) {
          countyNames[i] = arr[i].district;
          countys[i] = arr[i].code;
        }
        county = countys[0];
        // 更新数据  
        that.setData({
          countys: countys,
          countyNames: countyNames,
          // value: value,
        })
        that.getMultiArray();
      },
      fail: function (err) {

      },
    })
    if (countys.length == 0) {
      countys[0] = {
        name: ''
      };
      countyNames[0] = {
        name: ''
      };
    };

    that.setData({
      countys: countys,
      countyNames: countyNames,
    })
  },



  bindMultiPickerColumnChange: function (e) {
    var that = this;
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    if (e.detail.column == 0) {
      that.data.multiIndex = [e.detail.value, 0, 0];
    } else if (e.detail.column == 1) {
      //如果更新的是第二列“市”，第一列“省”的下标不变，第三列“区”的数组下标置为0
      that.data.multiIndex = [that.data.multiIndex[0], e.detail.value, 0];
    } else if (e.detail.column == 2) {
      //如果更新的是第三列“区”，第一列“省”和第二列“市”的值均不变。
      that.data.multiIndex = [that.data.multiIndex[0], that.data.multiIndex[1], e.detail.value];
    }
    if (e.detail.column == 0) {
      var val = e.detail.value;
      var arr = that.data.provinces[val];
      that.getCityArr(arr, val); //获取地级市数据
      this.setData({
        multiIndex: that.data.multiIndex,
      })
    } else if (e.detail.column == 1) {
      var val = e.detail.value;
      var arr = that.data.citys[val];
      that.getCountyInfo(arr, val); //获取区县数据
      this.setData({
        multiIndex: that.data.multiIndex,
      })
    } else if (e.detail.column == 2) {
      this.setData({
        multiIndex: that.data.multiIndex,
      })
    }
  },
  getMultiArray: function () {
    var that = this;
    console.log(that.data.countyNames.length);
    var multiArray = '[';
    for (var i = 0; i < that.data.provinceNames.length; i++) {
      multiArray += '{"city":[';
      for (var j = 0; j < that.data.cityNames.length; j++) {
        multiArray += '{"areas":[';
        for (var k = 0; k < that.data.countyNames.length; k++) {
          multiArray += '{"code":"' + that.data.countys[k] + '","name":"' + that.data.countyNames[k] + '"},';
        }
        multiArray = multiArray.substring(0, multiArray.length - 1);
        multiArray += '],"code":"' + that.data.citys[j] + '","name":"' + that.data.cityNames[j] + '"},';
      }
      multiArray = multiArray.substring(0, multiArray.length - 1);
      multiArray += '],"code":"' + that.data.provinces[i] + '","name":"' + that.data.provinceNames[i] + '"},';
    }
    multiArray = multiArray.substring(0, multiArray.length - 1);
    multiArray += ']';
    var multiArray1 = JSON.parse(multiArray);
    console.log(multiArray1);
    that.setData({
      multiArray: [multiArray1, multiArray1[0].city, multiArray1[0].city[0].areas]
    })

  },
  username: function (e) {
    var that = this;
    that.setData({
      username: e.detail.value
    })
  },
  telephone: function (e) {
    var that = this;
    that.setData({
      telephone: e.detail.value
    })
  },
  community: function (e) {
    var that = this;
    that.setData({
      community: e.detail.value
    })
  },
  city: function (e) {
    var that = this;
    that.setData({
      city: e.detail.value
    })
  },
  detailAddress: function (e) {
    var that = this;
    that.setData({
      detailAddress: e.detail.value
    })
  },
  remark: function (e) {
    var that = this;
    that.setData({
      remark: e.detail.value
    })
  },
  extract:function(){
    var that = this;
    wx.request({
      url: app.globalData.url + 'userApplyCommunity',
      method: 'GET',
      data: {
        userId: wx.getStorageSync("userId"),
        realName: that.data.username,
        telephone: that.data.telephone,
        community: that.data.community,
        detailAddress: that.data.detailAddress,
        address: that.data.name,
        site: that.data.longitude + ',' + that.data.latitude
      },
      success: function (res) {
        if (res.data.result == 1) {
          setTimeout(function () { // 做了个延时重试一次，作为保底。
            wx.showToast({
              title: '申请成功，请等待审核！',
              icon: "none",
              duration: 2000,      //停留时间
            })
          }, 500)
          wx.navigateBack({
            delta: 2
          })
        } else {
          wx.showToast({
            title: res.data.errmsg,
            icon: "none",
            duration: 2000,      //停留时间
          })
        }
        
      },
    });
  },
  goMapLocation: function () {
    wx.navigateTo({
      url: '/pages/mapLocation/mapLocation',
    })
  },
})